<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="keywords" content="Hexo Theme Keep">
    <meta name="description" content="Hexo Theme Keep">
    <meta name="author" content="cm_westwood">
    
    <title>
        
            第5周 |
        
        西木同学的代码训练 2022
    </title>
    
<link rel="stylesheet" href="/code_training_2022/css/style.css">

    <link rel="shortcut icon" href="">
    
<link rel="stylesheet" href="/code_training_2022/css/font-awesome.min.css">

    <script id="hexo-configurations">
    let KEEP = window.KEEP || {};
    KEEP.hexo_config = {"hostname":"cm_westwood.gitee.io","root":"/code_training_2022/","language":"en","path":"search.json"};
    KEEP.theme_config = {"toc":{"enable":true,"number":true,"expand_all":true,"init_open":true},"style":{"primary_color":"#0066CC","avatar":"","favicon":"","article_img_align":"left","left_side_width":"260px","content_max_width":"920px","hover":{"shadow":false,"scale":false},"first_screen":{"enable":true,"background_img":"https://xpoet.cn/images/bg.svg","description":"Keep writing and Keep loving."},"scroll":{"progress_bar":{"enable":true},"percent":{"enable":true}}},"local_search":{"enable":true,"preload":false},"code_copy":{"enable":true,"style":"default"},"pjax":{"enable":false},"lazyload":{"enable":true},"version":"3.4.5"};
    KEEP.language_ago = {"second":"%s seconds ago","minute":"%s minutes ago","hour":"%s hours ago","day":"%s days ago","week":"%s weeks ago","month":"%s months ago","year":"%s years ago"};
  </script>
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
<div class="progress-bar-container">
    
        <span class="scroll-progress-bar"></span>
    

    
</div>


<main class="page-container">

    

    <div class="page-main-content">

        <div class="page-main-content-top">
            <header class="header-wrapper">

    <div class="header-content">
        <div class="left">
            
            <a class="logo-title" href="/">
                西木同学的代码训练 2022
            </a>
        </div>

        <div class="right">
            <div class="pc">
                <ul class="menu-list">
                    
                        <li class="menu-item">
                            <a class=""
                               href="/code_training_2022/"
                            >
                                HOME
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/code_training_2022/archives"
                            >
                                ARCHIVES
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/code_training_2022/tags"
                            >
                                TAGS
                            </a>
                        </li>
                    
                    
                        <li class="menu-item search search-popup-trigger">
                            <i class="fas fa-search"></i>
                        </li>
                    
                </ul>
            </div>
            <div class="mobile">
                
                    <div class="icon-item search search-popup-trigger"><i class="fas fa-search"></i></div>
                
                <div class="icon-item menu-bar">
                    <div class="menu-bar-middle"></div>
                </div>
            </div>
        </div>
    </div>

    <div class="header-drawer">
        <ul class="drawer-menu-list">
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/code_training_2022/">HOME</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/code_training_2022/archives">ARCHIVES</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/code_training_2022/tags">TAGS</a>
                </li>
            
        </ul>
    </div>

    <div class="window-mask"></div>

</header>


        </div>

        <div class="page-main-content-middle">

            <div class="main-content">

                
                    <div class="fade-in-down-animation">
    <div class="article-content-container">

        <div class="article-title">
            <span class="title-hover-animation">第5周</span>
        </div>

        
            <div class="article-header">
                <div class="avatar">
                    <img src="">
                </div>
                <div class="info">
                    <div class="author">
                        <span class="name">cm_westwood</span>
                        
                            <span class="author-label">Lv2</span>
                        
                    </div>
                    <div class="meta-info">
                        <div class="article-meta-info">
    <span class="article-date article-meta-item">
        <i class="fas fa-edit"></i>&nbsp;
        <span class="pc">2022-07-23 09:27:14</span>
        <span class="mobile">2022-07-23 09:27</span>
    </span>
    
    
        <span class="article-tags article-meta-item">
            <i class="fas fa-tags"></i>&nbsp;
            <ul>
                
                    <li>
                        <a href="/code_training_2022/tags/Python/">Python</a>&nbsp;
                    </li>
                
                    <li>
                        | <a href="/code_training_2022/tags/Bazel/">Bazel</a>&nbsp;
                    </li>
                
                    <li>
                        | <a href="/code_training_2022/tags/Gunicorn/">Gunicorn</a>&nbsp;
                    </li>
                
            </ul>
        </span>
    

    
    
    
    
</div>

                    </div>
                </div>
            </div>
        

        <div class="article-content markdown-body">
            <p>python 的 retry；bazel deploy flask with gunicorn；python logging colorlog；python 中的偷天换日；python 中的 asyncio；python logging colorlog</p>
<span id="more"></span>

<p><a href="/code_training_2022/第5周/配套代码.html" style="text-align: center; display: block;">第5周的配套代码</a></p>
<h1 id="python-的-retry"><a href="#python-的-retry" class="headerlink" title="python 的 retry"></a>python 的 retry</h1><p>在训练过程中发现写的一些 request 函数会时不时的发生网络错误，这时候就需要超时重传了，逛了一圈，发现是 backofff 这个库比较好用，还支持异步重传。</p>
<blockquote>
<ol>
<li><a class="link"   target="_blank" rel="noopener" href="https://stackoverflow.com/questions/42521549/retry-function-in-python" >Retry function in Python - Stack Overflow<i class="fas fa-external-link-alt"></i></a></li>
<li><a class="link"   target="_blank" rel="noopener" href="https://github.com/litl/backoff" >litl&#x2F;backoff: Python library providing function decorators for configurable backoff and retry<i class="fas fa-external-link-alt"></i></a></li>
</ol>
</blockquote>
<h1 id="bazel-deploy-flask-with-gunicorn"><a href="#bazel-deploy-flask-with-gunicorn" class="headerlink" title="bazel deploy flask with gunicorn"></a>bazel deploy flask with gunicorn</h1><p>flask 是 python 中十分轻量级的一个 web 库，但是其本身的并发处理效率却不大行，需要借助守护程序进行托管 <a class="link"   target="_blank" rel="noopener" href="https://flask.net.cn/deploying/wsgi-standalone.html#gunicorn%EF%BC%8C%E8%B5%B0%E4%BA%86%E4%B8%80%E9%81%AD%EF%BC%8C%E5%8F%91%E7%8E%B0%E6%98%AF" >https://flask.net.cn/deploying/wsgi-standalone.html#gunicorn，走了一遭，发现是<i class="fas fa-external-link-alt"></i></a> Gunicorn 托管最 nice，一开始看这种 app 的命令行启动模式弄得我好迷糊，直到我看到了 <a class="link"   target="_blank" rel="noopener" href="https://docs.gunicorn.org/en/latest/run.html" >https://docs.gunicorn.org/en/latest/run.html<i class="fas fa-external-link-alt"></i></a> 这里说的一句话：</p>
<blockquote>
<p>Where <code>WSGI_APP</code> is of the pattern <code>$(MODULE_NAME):$(VARIABLE_NAME)</code>. The module name can be a full dotted path. The variable name refers to a WSGI callable that should be found in the specified module.</p>
</blockquote>
<p>原来是用 python module 的形式进行启动的，那我心里一切都敞亮了呀！</p>
<img  
                     lazyload
                     src="/images/loading.svg"
                     data-src="/code_training_2022/第5周/部署flask.png"
                      style="width:100%"
                >

<p>有了这个命令行启动的方式，就可以直接 bazel 奥利给开干了呀！直接采用 <a class="link"   target="_blank" rel="noopener" href="https://bazel.build/reference/be/shell#sh_binary" >Shell Rules  | Bazel<i class="fas fa-external-link-alt"></i></a> 的方法，打包一个程序，完美解决各种依赖。</p>
<blockquote>
<p><a class="link"   target="_blank" rel="noopener" href="https://flask.net.cn/deploying/wsgi-standalone.html#gunicorn" >https://flask.net.cn/deploying/wsgi-standalone.html#gunicorn<i class="fas fa-external-link-alt"></i></a></p>
<p><a class="link"   target="_blank" rel="noopener" href="https://docs.gunicorn.org/en/latest/run.html" >https://docs.gunicorn.org/en/latest/run.html<i class="fas fa-external-link-alt"></i></a></p>
<p><a class="link"   target="_blank" rel="noopener" href="https://bazel.build/reference/be/general" >General Rules  | Bazel<i class="fas fa-external-link-alt"></i></a></p>
<p><a class="link"   target="_blank" rel="noopener" href="https://bazel.build/reference/be/shell#sh_binary" >Shell Rules  | Bazel<i class="fas fa-external-link-alt"></i></a></p>
</blockquote>
<h1 id="python-中的偷天换日"><a href="#python-中的偷天换日" class="headerlink" title="python 中的偷天换日"></a>python 中的偷天换日</h1><p>合理的利用这两个技术，就能够让我们在给 python 模块写测试用例的时候，使用一些 fake modules 去替代原有的下游程序模块，不费一兵一卒，一个 main 函数就能完成代码的测试。</p>
<blockquote>
<p><a class="link"   target="_blank" rel="noopener" href="https://stackoverflow.com/questions/6965090/how-to-add-builtin-functions" >https://stackoverflow.com/questions/6965090/how-to-add-builtin-functions<i class="fas fa-external-link-alt"></i></a><br><a class="link"   target="_blank" rel="noopener" href="https://stackoverflow.com/questions/15395988/fake-module-used-by-other-modules" >https://stackoverflow.com/questions/15395988/fake-module-used-by-other-modules<i class="fas fa-external-link-alt"></i></a></p>
</blockquote>
<h1 id="python-中的-asyncio"><a href="#python-中的-asyncio" class="headerlink" title="python 中的 asyncio"></a>python 中的 asyncio</h1><p>首先把协程的这个理念搞懂：</p>
<blockquote>
 <img  
                     lazyload
                     src="/images/loading.svg"
                     data-src="/code_training_2022/第5周/协程与线程进程的区别.webp"
                      style="width:100%"
                >
</blockquote>
<p>然后跟着官方教程敲一遍：</p>
<blockquote>
<p><a class="link"   target="_blank" rel="noopener" href="https://docs.python.org/zh-cn/3/library/asyncio-task.html" >https://docs.python.org/zh-cn/3/library/asyncio-task.html<i class="fas fa-external-link-alt"></i></a></p>
</blockquote>
<p>协程的一个好处就是可以很方便的等待 callback 的函数调用，如：</p>
<blockquote>
<p><a class="link"   target="_blank" rel="noopener" href="https://stackoverflow.com/questions/51328952/python3-asyncio-await-on-callback" >python 3.x - python3 asyncio等待回调 - 代码日志<i class="fas fa-external-link-alt"></i></a></p>
</blockquote>
<p>但是在 IPython 和 Jupyter notebook 下运行代码的时候，后台已经起了一个 asyncio 的 loop，</p>
<blockquote>
<p><a class="link"   target="_blank" rel="noopener" href="https://ipython.readthedocs.io/en/stable/interactive/autoawait.html" >Asynchronous in REPL: Autoawait — IPython 8.4.0 documentation<i class="fas fa-external-link-alt"></i></a><br><a class="link"   target="_blank" rel="noopener" href="https://github.com/python-trio/trio" >python-trio&#x2F;trio: Trio – a friendly Python library for async concurrency and I&#x2F;O<i class="fas fa-external-link-alt"></i></a></p>
</blockquote>
<p>所以会出现一些意料之外的情况，如：</p>
<blockquote>
<p><a class="link"   target="_blank" rel="noopener" href="https://stackoverflow.com/questions/36895256/coroutine-as-background-job-in-jupyter-notebook" >python - Coroutine as background job in Jupyter notebook - Stack Overflow<i class="fas fa-external-link-alt"></i></a></p>
<p><a class="link"   target="_blank" rel="noopener" href="https://github.com/ipython/ipython/issues/12786" >AsyncIO can cause deadlocks in the InteractiveShell - Impossible to wait on Comm events · Issue #12786 · ipython&#x2F;ipython<i class="fas fa-external-link-alt"></i></a></p>
</blockquote>
<h1 id="python-logging-colorlog"><a href="#python-logging-colorlog" class="headerlink" title="python logging colorlog"></a>python logging colorlog</h1><p>集百家之所长，摸索了一个最简洁的 colorlog 的代码</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> logging</span><br><span class="line"><span class="keyword">import</span> colorlog</span><br><span class="line"><span class="keyword">import</span> importlib</span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">basicConfig</span>(<span class="params">formatter = <span class="string">&quot;%(levelname)-8s | %(message)s&quot;</span>, **kwargs</span>):</span><br><span class="line">    importlib.reload(logging)</span><br><span class="line">    handler = colorlog.StreamHandler()</span><br><span class="line">    handler.setFormatter(colorlog.ColoredFormatter(<span class="string">f&#x27;%(log_color)s<span class="subst">&#123;formatter&#125;</span>%(reset)s&#x27;</span>))</span><br><span class="line">    handler.setLevel(**kwargs)</span><br><span class="line">    logging.basicConfig(</span><br><span class="line">        handlers=[handler],</span><br><span class="line">        **kwargs,</span><br><span class="line">    )</span><br></pre></td></tr></table></figure>

<img  
                     lazyload
                     src="/images/loading.svg"
                     data-src="/code_training_2022/第5周/colorlog.png"
                      style="width:100%"
                >

<blockquote>
<p><a class="link"   target="_blank" rel="noopener" href="https://stackoverflow.com/questions/384076/how-can-i-color-python-logging-output" >How can I color Python logging output? - Stack Overflow<i class="fas fa-external-link-alt"></i></a></p>
</blockquote>

        </div>

        

        
            <ul class="post-tags-box">
                
                    <li class="tag-item">
                        <a href="/code_training_2022/tags/Python/">#Python</a>&nbsp;
                    </li>
                
                    <li class="tag-item">
                        <a href="/code_training_2022/tags/Bazel/">#Bazel</a>&nbsp;
                    </li>
                
                    <li class="tag-item">
                        <a href="/code_training_2022/tags/Gunicorn/">#Gunicorn</a>&nbsp;
                    </li>
                
            </ul>
        

        
            <div class="article-nav">
                
                    <div class="article-prev">
                        <a class="prev"
                           rel="prev"
                           href="/code_training_2022/2022/08/20/%E7%AC%AC6%E5%91%A8/"
                        >
                            <span class="left arrow-icon flex-center">
                              <i class="fas fa-chevron-left"></i>
                            </span>
                            <span class="title flex-center">
                                <span class="post-nav-title-item">第6周</span>
                                <span class="post-nav-item">Prev posts</span>
                            </span>
                        </a>
                    </div>
                
                
                    <div class="article-next">
                        <a class="next"
                           rel="next"
                           href="/code_training_2022/2022/07/17/%E7%AC%AC4%E5%91%A8/"
                        >
                            <span class="title flex-center">
                                <span class="post-nav-title-item">第4周</span>
                                <span class="post-nav-item">Next posts</span>
                            </span>
                            <span class="right arrow-icon flex-center">
                              <i class="fas fa-chevron-right"></i>
                            </span>
                        </a>
                    </div>
                
            </div>
        

        
    </div>
</div>


                
            </div>

        </div>

        <div class="page-main-content-bottom">
            <footer class="footer">
    <div class="info-container">
        <div class="copyright-info info-item">
            &copy;
            
              <span>2022</span>
              -
            
            2023&nbsp;<i class="fas fa-heart icon-animate"></i>&nbsp;<a href="/">cm_westwood</a>
        </div>
        
        <div class="theme-info info-item">
            Powered by <a target="_blank" href="https://hexo.io">Hexo</a>&nbsp;|&nbsp;Theme&nbsp;<a class="theme-version" target="_blank" href="https://github.com/XPoet/hexo-theme-keep">Keep v3.4.5</a>
        </div>
        
        
    </div>
</footer>

        </div>
    </div>

    
        <div class="post-tools">
            <div class="post-tools-container">
    <ul class="tools-list">
        <!-- TOC aside toggle -->
        
            <li class="tools-item page-aside-toggle">
                <i class="fas fa-outdent"></i>
            </li>
        

        <!-- go comment -->
        
    </ul>
</div>

        </div>
    

    <div class="right-bottom-side-tools">
        <div class="side-tools-container">
    <ul class="side-tools-list">
        <li class="tools-item tool-font-adjust-plus flex-center">
            <i class="fas fa-search-plus"></i>
        </li>

        <li class="tools-item tool-font-adjust-minus flex-center">
            <i class="fas fa-search-minus"></i>
        </li>

        <li class="tools-item tool-expand-width flex-center">
            <i class="fas fa-arrows-alt-h"></i>
        </li>

        <li class="tools-item tool-dark-light-toggle flex-center">
            <i class="fas fa-moon"></i>
        </li>

        <!-- rss -->
        

        

        <li class="tools-item tool-scroll-to-bottom flex-center">
            <i class="fas fa-arrow-down"></i>
        </li>
    </ul>

    <ul class="exposed-tools-list">
        <li class="tools-item tool-toggle-show flex-center">
            <i class="fas fa-cog fa-spin"></i>
        </li>
        
            <li class="tools-item tool-scroll-to-top flex-center">
                <i class="arrow-up fas fa-arrow-up"></i>
                <span class="percent"></span>
            </li>
        
    </ul>
</div>

    </div>

    
        <aside class="page-aside">
            <div class="post-toc-wrap">
    <div class="post-toc">
        <ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#python-%E7%9A%84-retry"><span class="nav-number">1.</span> <span class="nav-text">python 的 retry</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#bazel-deploy-flask-with-gunicorn"><span class="nav-number">2.</span> <span class="nav-text">bazel deploy flask with gunicorn</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#python-%E4%B8%AD%E7%9A%84%E5%81%B7%E5%A4%A9%E6%8D%A2%E6%97%A5"><span class="nav-number">3.</span> <span class="nav-text">python 中的偷天换日</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#python-%E4%B8%AD%E7%9A%84-asyncio"><span class="nav-number">4.</span> <span class="nav-text">python 中的 asyncio</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#python-logging-colorlog"><span class="nav-number">5.</span> <span class="nav-text">python logging colorlog</span></a></li></ol>
    </div>
</div>
        </aside>
    

    <div class="image-viewer-container">
    <img src="">
</div>


    
        <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
          <span class="search-input-field-pre">
            <i class="fas fa-keyboard"></i>
          </span>
            <div class="search-input-container">
                <input autocomplete="off"
                       autocorrect="off"
                       autocapitalize="off"
                       placeholder="Search..."
                       spellcheck="false"
                       type="search"
                       class="search-input"
                >
            </div>
            <span class="popup-btn-close">
                <i class="fas fa-times"></i>
            </span>
        </div>
        <div id="search-result">
            <div id="no-result">
                <i class="fas fa-spinner fa-pulse fa-5x fa-fw"></i>
            </div>
        </div>
    </div>
</div>

    

</main>




<script src="/code_training_2022/js/utils.js"></script>

<script src="/code_training_2022/js/main.js"></script>

<script src="/code_training_2022/js/header-shrink.js"></script>

<script src="/code_training_2022/js/back2top.js"></script>

<script src="/code_training_2022/js/dark-light-toggle.js"></script>



    
<script src="/code_training_2022/js/local-search.js"></script>




    
<script src="/code_training_2022/js/code-copy.js"></script>




    
<script src="/code_training_2022/js/lazyload.js"></script>



<div class="post-scripts">
    
        
<script src="/code_training_2022/js/left-side-toggle.js"></script>

<script src="/code_training_2022/js/libs/anime.min.js"></script>

<script src="/code_training_2022/js/toc.js"></script>

    
</div>



</body>
</html>
